package org.bigk.invoices.actions.ajax;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.bigk.invoices.actions.InvoicesActionSupport;
import org.bigk.invoices.model.Invoice;
import org.bigk.invoices.services.PaymentKindsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


@Controller("paymentDateAction")
@Namespace("/nodecorate")
public class PaymentDateAction extends InvoicesActionSupport {
	
	private static final long serialVersionUID = 1558037045535040777L;
	private static final Logger logger = LoggerFactory.getLogger(PaymentDateAction.class);
	
	private Invoice model;
	
	@Autowired
	protected PaymentKindsService paymentKindsService;

	@Action(value = "PaymentDateAction",
			results = {
				@Result(name = SUCCESS, location = "/pages/ajax/paymentDateAction.jsp")
			}
	)
	public String execute() throws Exception {
		logger.debug("execute() - start");
		logger.debug("execute() - model=[{}]", model);

		Long pkid = model.getPaymentKindId();
		if (pkid != null) {
			Date paymentDate = paymentKindsService.calculatePaymentDate(model.getSoldDate(), pkid);
			model.setPaymentDate(paymentDate);
		}
		
		logger.debug("execute() - end - return value=[{}]", SUCCESS);
		return SUCCESS;
	}

	public Invoice getModel() {
		return model;
	}

	public void setModel(Invoice model) {
		this.model = model;
	}
}
